<%-- 
    Document   : searchTask
    Created on : Jul 11, 2012, 10:19:19 AM
    Author     : ThachKim
--%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

<style type="text/css">
    @import url("css/general.css");
</style>

<html>
    <head>
        <script type="text/javascript">
            function updateTasks(){
                var validate = true;
                var formList = document.forms("TaskForm");
                formList.length;
                var string="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
                string=string+"<tasks>";
                for (var i=1; i<formList.length; i++){
                    string=string+"<task>";
                    string=string+"<taskID>";
                    var taskID = formList[i].taskID.value;

                    string=string+taskID;
                    string=string+"</taskID>";

                    string=string+"<taskName>";
                    var taskName = formList[i].taskName.value;
                    string=string+taskName;
                    string=string+"</taskName>";

                    if (taskName == "")
                    {
                        window.alert("Task Name NOT Empty!");
                        validate = false;
                        break;
                    }

                    string=string+"<minestoneID>";
                    var minestoneID = formList[i].minestoneID.value;
                    string=string+minestoneID;
                    string=string+"</minestoneID>";

                    string=string+"<username>";
                    var username =formList[i].username.value;
                    string=string+username;
                    string=string+"</username>";

                    string=string+"<description>";
                    var description= formList[i].description.value;
                    string=string+description;
                    string=string+"</description>";
                    if (description == "")
                    {
                        window.alert("Description NOT Empty!");
                        validate = false;
                        break;
                    }
                    string=string+"<startDay>";
                    string=string+"<year>";
                    var sdyear = formList[i].syearId.value;
                    string=string+sdyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var sdmonth= formList[i].smonthId.value;
                    string=string+sdmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var sdday= formList[i].sdayId.value;
                    string=string+sdday;
                    string=string+"</day>";
                    string=string+"</startDay>";

                    string=string+"<endDay>";
                    string=string+"<year>";
                    var edyear= formList[i].eyearId.value;
                    string=string+edyear;
                    string=string+"</year>";

                    string=string+"<month>";
                    var edmonth= formList[i].emonthId.value;
                    string=string+edmonth;
                    string=string+"</month>";

                    string=string+"<day>";
                    var edday= formList[i].edayId.value;
                    string=string+edday;
                    string=string+"</day>";
                    string=string+"</endDay>";

                    string=string+"<status>";
                    var status=formList[i].status.value;
                    string=string+status;
                    string=string+"</status>";

                    string=string+"</task>";

                    if (Number(sdyear) > Number(edyear))
                    {
                        validate = false;
                        window.alert("Error: Tasks '" + taskName +"' have Start Day BIGGER End Day !");
                        break;
                    } else if (Number(sdyear) == Number(edyear) && Number(sdmonth) > Number(edmonth))
                    {
                        validate = false;
                        window.alert("Error: Tasks '" + projectName +"' have Start Day BIGGER End Day !");
                        break;
                    }else if (Number(sdyear) == Number(edyear) && Number(sdmonth) == Number(edmonth) && Number(sdday) > Number(edday))
                    {
                        validate = false;
                        window.alert("Error: Tasks '" + projectName +"' have Start Day BIGGER End Day !");
                        break;
                    }
                }
                if (validate)
                {
                    string=string+"</tasks>";
                    if(window.ActiveXObject)//IE
                        xml_req = new ActiveXObject("Microsoft.XMLHTTP");
                    else if(window.XMLHttpRequest)
                        xml_req = new XMLHttpRequest();
                    else alert('no ajax support');

                    xml_req = new XMLHttpRequest();
                    xml_req.open("POST", "UpdateTasks", true);
                    xml_req.setRequestHeader('Content-Type','text/xml');
                    xml_req.send(string);
                    xml_req.onreadystatechange = handleUpdateTasksResponse;
                }
            }

            function handleUpdateTasksResponse() {
                if (xml_req.readyState == 4) {
                    if (xml_req.status == 200) {
                        window.alert("Update Task table successfull!!");
                    }else
                        window.alert("Update Task table NOT successfull!!");
                }else {
                }
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Staff Main Page</title>
    </head>
    <body>
        <div id="wrapper">
            <div id="header">
                <div id="user">
                    ${sessionScope.FULLNAME}, <a href="Controller?action=logout">Log out</a>
                </div>
            </div>

            <div id="body">
                <div id="title">
                    <h2 align="center">Staff Main Page</h2>
                </div>
                <div id="content">
                    <div id="left">
                        <table id="Function Reference:" width="200px">
                            <tr>
                                <th>Main Functions:</th>
                            </tr>
                            <tr>
                                <td><a href="staffSearch.jsp">Search task</a></td>
                            </tr>                           
                        </table>
                    </div>
                    <div id="right">
                        <div id="titleRight">
                            <h2 align="center">Update Tasks Information</h2>
                        </div>
                        <div id="contenRight">
                            <h3 align="left">Update Tasks
                            </h3>
                            <c:import url="xml/Tasks.xml" var="xmldoc"/>
                            <c:import url="xsl/Task.xsl" var="xsldoc"/>
                            <x:transform xml="${xmldoc}" xslt="${xsldoc}">
                            </x:transform>
                            <input align="middle" type="button" name="Update Tasks" value="Update Tasks" onclick="updateTasks();" />
                        </div>
                    </div>
                </div>
            </div>

            <div id="footer">

            </div>
        </div>
    </body>
</html>
